package com.cee.admin.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *     地图坐标工具
 * <p>
 *
 * @author hys
 * @since 2024/5/24 15:12
 */
@Component
@Slf4j
public class MapCoordinateUtil {

    private static final Map<String, String> mapCoordinateData = new HashMap<>(512);
    private static int sum = 0;
    static {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try{
            String pattern = "classpath*:/mapCoordinateJsonFile/**";
            Resource[] resources = resolver.getResources(pattern);

            for (Resource resource : resources) {
                if (resource.isReadable()) { // 确保文件可读
                    String filename = resource.getFilename();
                    try (InputStream inputStream = new BOMInputStream(resource.getInputStream())) {
                        String coordinateData = IOUtils.toString(inputStream);
                        if (filename != null) {
                            mapCoordinateData.put(filename.substring(0, filename.lastIndexOf(".")), coordinateData);
                        }
                        sum++;
//                        log.debug("文件名：" + filename);
                    } catch (IOException e) {
                        log.error(e.getMessage(), e);
                        throw new RuntimeException("加载地图坐标文件 " + filename + " 出错：" + e.getMessage());
                    }
                }
            }
            log.debug("已加载地图坐标文件数量：{}", sum);
        }catch(Exception e){
            log.error(e.getMessage(),e);
            throw new RuntimeException("加载地图坐标文件失败，请检查文件是否存在");
        }
    }

    /**
     * 获取地图坐标信息
     * @param name 地点名称
     * @return 地图坐标信息
     */
    public static String get(String name) {
        return mapCoordinateData.get(name);
    }

    public static void main(String[] args) {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try{
            String pattern = "classpath*:/mapCoordinateJsonFile/**";
            Resource[] resources = resolver.getResources(pattern);

            for (Resource resource : resources) {
                if (resource.isReadable()) { // 确保文件可读
                    String filename = resource.getFilename();

                    try (InputStream inputStream = resource.getInputStream()) {
                        String coordinateData = IOUtils.toString(new BOMInputStream(inputStream));
                        if (filename != null) {
                            mapCoordinateData.put(filename.substring(0, filename.lastIndexOf(".")), coordinateData);
                        }
                        sum++;
                        log.warn("文件名：" + filename);
                        log.warn("文件路径：" + resource.getURL());
                    } catch (IOException e) {
                        log.error(e.getMessage(),e);
                        throw new RuntimeException("加载地图坐标文件 " + filename + " 出错：" + e.getMessage());
                    }
                }
            }
            log.error("地图坐标文件数量：{}", sum);
        }catch(Exception e){
            log.error(e.getMessage(),e);
            throw new RuntimeException("加载地图坐标文件失败，请检查文件是否存在");
        }
    }

}
